package com.shop.controller;

import com.github.pagehelper.Page;
import com.shop.domain.Order;
import com.shop.entity.PageResult;
import com.shop.entity.Result;
import com.shop.entity.StatusCode;
import com.shop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Order>> selectAll(){
        return new Result<>("查询成功",orderService.selectAll());
    }

    /**
     * 条件查询
     * @param order
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<Order>> selectList(@RequestBody Order order){
        return new Result<>("条件查询成功",orderService.selectList(order));
    }

    /**
     * 新增订单
     * @param order
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Order order){
        Integer i = orderService.insert(order);
        if(i == -1){
            return Result.fail("库存不足");
        }else if(i == -2){
            return Result.fail("下单失败");
        } else if (i == -3) {
            return Result.fail("减库存失败");
        }
        return Result.ok("下单成功");
    }

    /**
     * 删除订单
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        orderService.delete(ids);
        return new Result<>("删除成功");
    }

    /**
     * 修改订单
     * @param order
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Order order){
        orderService.update(order);
        return new Result("修改成功");
    }

    /**
     * 分页搜索
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/pageAll")
    public Result pageAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer size){
        Page<Order> pageList = orderService.pageAll(page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 条件分页搜素
     * @param order
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/pageList")
    public Result pageList(@RequestBody Order order,@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer size){
        Page<Order> pageList = orderService.pageList(order,page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 批量发货
     * @param orders
     * @return
     */
    @PostMapping("/batchSend")
    public Result batchSend(@RequestBody List<Order> orders){
        orderService.batchSend(orders);
        return Result.ok("发货成功");
    }

    /**
     * 确认收货
     * @param id
     * @return
     */
    @GetMapping("/take")
    public Result take(@RequestParam String id){
        orderService.confirmTask(id);
        return new Result( true,StatusCode.OK,"" );
    }
}
